package cn.itcast.service.impl;

import cn.itcast.dao.PermissionDao;
import cn.itcast.entity.PageResult;

import cn.itcast.pojo.Permission;
import cn.itcast.service.PermissionService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;

    @Override
    public PageResult findByPage(Integer currentPage, Integer pageSize, String queryString) {
        //开启分页查询
        PageHelper.startPage(currentPage, pageSize);
        //根据条件分页查询检查项信息
        Page<Permission> page = permissionDao.selectByCondition(queryString);
        //构建分页数据返回
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Boolean save(Permission permission) {
        return permissionDao.save(permission) > 0;
    }

    @Override
    public Boolean delete(Integer permissionId) {
        Integer count = permissionDao.findRoleIdByPermissionId(permissionId);
        count += permissionDao.findMenuIdByPermissionId(permissionId);
        if (count>0){
            return false;
        }
        return permissionDao.delete(permissionId) > 0;
    }

    @Override
    public Boolean edit(Permission permission) {
        return permissionDao.edit(permission) > 0;
    }

    @Override
    public Permission findById(Integer id) {
        return permissionDao.findByid(id);
    }

    @Override
    public List<Permission> findByMenuIds(List<Integer> menuIds) {
        return permissionDao.findByMenuIds(menuIds);
    }

    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }


}
